package gamePackage;

//Imports
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class DrawPanel extends JPanel implements ActionListener {
	
	Player player; //Player class variable
	Image bg; //Background image variable
	Timer time; //FPS update
	
	//Constructor
	public DrawPanel() {
		
		player = new Player();
		addKeyListener(new actionKey());
		setFocusable(true); //Allow detect key press movement
		
		//Retrieve background image
		ImageIcon imgGet = new ImageIcon("game/caveBG.png");
		bg = imgGet.getImage();
		
		//Variable for image update
		time = new Timer(5, this);
		time.start();
		
	} //End constructor
	
	public void actionPerformed(ActionEvent e) { //Action listener repaint method
		
		player.move();
		repaint();
		
	} //End action listener method
	
	public void paint(Graphics g) { //2D graphics
		
		super.paint(g);
		Graphics2D graphics = (Graphics2D) g;
		graphics.drawImage(bg, 0, 0, null); //Background position
		graphics.drawImage(player.getPlay(), player.playerX(), player.playerY(), null); //Player position
		
	} //End 2D graphics
	
	private class actionKey extends KeyAdapter { //Detect key press
		
		public void keyReleased(KeyEvent e) { //Key up method
			
			player.keyReleased(e);
			
		} //End method
		
		public void keyPressed(KeyEvent e) { //Key down method
			
			player.keyPressed(e);
			
		} //End method
		
	} //End private class
	
}